import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;


public class themeThread extends myThread
{

	public themeThread(MyJFrame f, JTextArea output,
			ArrayList<JComponent> comp)
	{
		super(f, output, comp);
	}

	@Override
	public boolean body()
	{
		//Get zip file containing theme
		String zipOut="out";
		if (JOptionPane.showConfirmDialog(f, "To add theme, you need to have a zip file containing the theme you want (in a source format)\nAlso you should know to which file it belongs\nAnd what files need to be replaced.\nTo continue press yes","Theme confirmations",JOptionPane.YES_NO_OPTION)!=0)
			return false;
		JOptionPane.showMessageDialog(f, "Please select theme zip file");
		JFileChooser chooser=new JFileChooser();
		chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
		chooser.setDialogTitle("Theme Zip file");
		chooser.setFileFilter(new ZipFilter());
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.showDialog(f, "OK");
		if (chooser.getSelectedFile()==null)
		{
			Log.debug("no theme file was selected");
			return false;
		}
		output.append("Extracrting file");
		File theme=chooser.getSelectedFile();
		if (!HelpFunctions.extractZipToDest(theme.getAbsolutePath(), zipOut))
			return false;
		
		//Get rom's file to patch
		JOptionPane.showMessageDialog(f, "Choose the file (in your rom folder) you want to patch");
		chooser.setCurrentDirectory(new File(params.activeProject));
		chooser.setDialogTitle("source file");
		chooser.setFileFilter(new AndroidFilter());
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.showDialog(f, "OK");
		if (chooser.getSelectedFile()==null)
		{
			Log.debug("no file was selected as rom base file");
			return false;
		}
		AndroidFile and=new AndroidFile(chooser.getSelectedFile().getAbsolutePath());
		output.append("Decompiling source file");
		if (!and.decompile())
		{
			Log.error("failed to decompile: "+and.getAbsolutePath());
			return false;
		}
		
		JOptionPane.showMessageDialog(f, "Please select folder in extracted zip archive\nWhich will be patched");
		chooser.setCurrentDirectory(new File(zipOut));
		chooser.setDialogTitle("Theme sources to use");
		chooser.setFileFilter(null);
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		chooser.showDialog(f, "OK");
		if (chooser.getSelectedFile()==null)
		{
			Log.debug("no file was selected as source from zip file");
			return false;
		}
		File themeDir=chooser.getSelectedFile();
		
		JOptionPane.showMessageDialog(f, "Please select matching files in rom file sources");
		chooser.setCurrentDirectory(new File(and.getDecompiledPath()));
		chooser.setDialogTitle("Rom sources to replace");
		chooser.setFileFilter(null);
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		chooser.showDialog(f, "OK");
		if (chooser.getSelectedFile()==null)
		{
			Log.debug("no file was selected as rom source to patch");
			return false;
		}
		File sourceDir=chooser.getSelectedFile();
		try {
			HelpFunctions.copyDirectory(themeDir, sourceDir);
		} catch (IOException e) {
			Log.writeEx(e);
		}
		output.append("compiling");
		if (and.compile()==null)
		{
			Log.error("could not compile patched theme file");
			output.append("failed to compile");
			return false;
		}
		and.replaceOriginalCompiled();
		and.clear();
		new File(zipOut).delete();
		output.append("finished patching theme. WARNING: theme may cause rom to be unstable. please verify that it actually works. If it doesn't work please report");
		
		return false;
	}

}
